package Comprehensive_01;

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class Test3 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] arr = new int[getLength()];
        assignment(arr);
        int odds = getOdds(arr);
        int eventSum = getEventSum(arr);
        double oddsAverage = getOddsAverage(arr);
        System.out.println(Arrays.toString(arr));
        System.out.println("数组中偶数的个数" + odds);
        System.out.println("数组中奇数的和" + eventSum);
        System.out.println("数组中偶数的平均值" + oddsAverage);
        System.out.println("请输入要查找的整数：");
        int number = sc.nextInt();
        int[] results = arrSearchNumber(arr, number);
        System.out.println(Arrays.toString(results));

    }

    public static int getLength() {
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请输入一个4~7的整数：");
            int number = sc.nextInt();
            if (number < 4 || number > 7) {
                System.out.println("您输入的数字不合法，请重新输入");
                continue;
            }
            return number;
        }
    }

    public static void assignment(int[] arr) {
        Random random = new Random();
        for (int i = 0; i < arr.length; i++) {
            arr[i] = random.nextInt(61) + 20;
        }
    }

    public static int getOdds(int[] arr) {
        int count = 0;
        for (int i : arr) {
            if (i % 2 == 0) {
                count++;
            }
        }
        return count;
    }

    public static int getEventSum(int[] arr) {
        int sum = 0;
        for (int i : arr) {
            if (i % 2 != 0) {
                sum += i;
            }
        }
        return sum;
    }

    public static double getOddsAverage(int[] arr) {
        int sum = 0;
        for (int odd : arr) {
            if (odd % 2 == 0) {
                sum += odd;
            }
        }
        return (double) sum / getOdds(arr);
    }

    public static int getIndex(int[] arr, int number) {
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            if (number == arr[i]) {
                count++;
            }
        }
        return count;
    }

    public static int[] arrSearchNumber(int[] arr, int number) {
        int[] newArr = new int[getIndex(arr, number)];
        if (newArr.length == 0) {
            return new int[]{-1};
        }
        int index = 0;
        for (int j = 0; j < arr.length; j++) {
            if (number == arr[j]) {
                newArr[index] = j;
                index++;
            }
        }
        return newArr;
    }

}
